﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : MultiServerEngine | PhotonTestClient | MainWindow.xaml.cs
// //  Created : 2016 - 02 - 16 20:53
// //  Modified: 2016 - 02 - 20 12:33
// //  ********************************************************************************

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using ExitGames.Client.Photon;
using PhotonTestClient.Dialogs;
using TypeDefs;

namespace PhotonTestClient
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PhotonPeer Peer { get; }
        public bool PeerConnecting { get; set; }
        public bool PeerConnected { get; set; }
        public DispatcherTimer DispatcherTimer { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            PeerConnecting = false;
            PeerConnected = false;
            PhotonListener listener = new PhotonListener {Main = this};
            Peer = new PhotonPeer(listener, ConnectionProtocol.Udp);
            Disconnect_Button.IsEnabled = false;
            DispatcherTimer = new DispatcherTimer();
            DispatcherTimer.Tick += dispatcherTimer_tick;
            DispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
            StatusIndicator.Fill = new SolidColorBrush(Colors.Red);
            StatusTextBox.Foreground = new SolidColorBrush(Colors.Black);
        }

        private void Connect_Button_OnClick(object sender, RoutedEventArgs e)
        {
            StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - Trying to connect to " +
                                     IPAddress.Text + " on default port 5055...\n");
            Connect_Button.IsEnabled = false;
            if (Peer.Connect(IPAddress.Text + ":5055", "MasterServer"))
            {
                PeerConnecting = true;
                StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") +
                                         " - Connection initialization in progress...\n");
                DispatcherTimer.Start();
            }
            else
            {
                StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") +
                                         " - Connection initialization failed...\n");
            }
        }

        private void Disconnect_Button_OnClick(object sender, RoutedEventArgs e)
        {
            StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - Trying to disconnect from " +
                                     IPAddress.Text + "...\n");
            Peer.Disconnect();
        }

        private void dispatcherTimer_tick(object sender, EventArgs e)
        {
            Service();
            if (PeerConnected)
            {
                StatusIndicator.Fill = new SolidColorBrush(Colors.Green);
            }
            else if (PeerConnecting && !PeerConnected)
            {
                StatusIndicator.Fill = new SolidColorBrush(Colors.Orange);
            }
            else
            {
                StatusIndicator.Fill = new SolidColorBrush(Colors.Red);
            }
            CommandManager.InvalidateRequerySuggested();
        }

        private void Service()
        {
            Peer.Service();
        }

        public void RegisterAccount(string givenName, string surName, string eMail, string pass)
        {
            OperationRequest operation = new OperationRequest();
            Dictionary<byte, object> parameters = new Dictionary<byte, object>
                                                  {
                                                      {
                                                          (byte) ParameterCodes.SubCode,
                                                          OperationSubCodes.RegisterAccount
                                                      },
                                                      {(byte) ParameterCodes.GivenName, givenName},
                                                      {(byte) ParameterCodes.Surname, surName},
                                                      {(byte) ParameterCodes.LoginName, eMail},
                                                      {(byte) ParameterCodes.LoginPass, pass}
                                                  };

            operation.OperationCode = (byte) OperationCodes.Login;
            operation.Parameters = parameters;

            Peer.OpCustom(operation, true, 0, false);
        }

        public void LoginAccount(string user, string pass)
        {
            OperationRequest operation = new OperationRequest();
            Dictionary<byte, object> parameters = new Dictionary<byte, object>
                                                  {
                                                      {
                                                          (byte) ParameterCodes.SubCode,
                                                          OperationSubCodes.LoginAccount
                                                      },
                                                      {(byte) ParameterCodes.LoginName, user},
                                                      {(byte) ParameterCodes.LoginPass, pass}
                                                  };
            operation.OperationCode = (byte) OperationCodes.Login;
            operation.Parameters = parameters;

            Peer.OpCustom(operation, true, 0, false);
        }

        public void DeleteAccount(string user, string pass)
        {
            OperationRequest operation = new OperationRequest();
            Dictionary<byte, object> parameters = new Dictionary<byte, object>
                                                  {
                                                      {
                                                          (byte) ParameterCodes.SubCode,
                                                          OperationSubCodes.DeleteAccount
                                                      },
                                                      {(byte) ParameterCodes.LoginName, user},
                                                      {(byte) ParameterCodes.LoginPass, pass}
                                                  };
            operation.OperationCode = (byte) OperationCodes.Login;
            operation.Parameters = parameters;

            Peer.OpCustom(operation, true, 0, false);
        }

        private void LoginDialog_OnClick(object sender, RoutedEventArgs e)
        {
            Login dialog = new Login {Main = this, Owner = this};

            if (dialog.ShowDialog().Equals(true))
            {
            }
        }

        private void RegisterDialog_OnClick(object sender, RoutedEventArgs e)
        {
            Register dialog = new Register {Main = this, Owner = this};

            if (dialog.ShowDialog().Equals(true))
            {
            }
        }

        private void DeleteDialog_OnClick(object sender, RoutedEventArgs e)
        {
            Delete dialog = new Delete {Main = this, Owner = this};
            if (dialog.ShowDialog().Equals(true))
            {
            }
        }

        private void GetRealmDialog_OnClick(object sender, RoutedEventArgs e)
        {
            OperationRequest operation = new OperationRequest
            {
                OperationCode = (byte)OperationCodes.Transfer,
                Parameters =
                                                 new Dictionary<byte, object>()
                                                 {
                                                     {
                                                         (byte)
                                                         ParameterCodes
                                                         .SubCode,
                                                         OperationSubCodes
                                                         .RequestRealmList
                                                     }
                                                 }
            };
            Peer.OpCustom(operation, true, 0, false);
        }
    }
}
